package evs.location;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Converter
{
    public static <T> T convertTo( String value, Class<T> type ) throws EvsException
    {
        if ( type.equals( String.class ) )
            return type.cast( value );
        
        if ( type.equals( Long.class ) || type.equals( long.class ) )
            return type.cast( Long.parseLong( value ) );
        
        if ( type.equals( Integer.class ) || type.equals( int.class ) )
            return type.cast( Integer.parseInt( value ) );
        
        if ( type.equals( Short.class ) || type.equals( short.class ) )
            return type.cast( Short.parseShort( value ) );
        
        if ( type.equals( Character.class ) || type.equals( char.class ) )
            return type.cast( value.charAt( 0 ) );
        
        if ( type.equals( Byte.class ) || type.equals( byte.class ) )
            return type.cast( Byte.parseByte( value ) );
            
        if ( type.isEnum() )
        {
            return _convertEnum( value, type );
        }
        
        if ( type.equals( Date.class ) )
        {
            try
            {
                return type.cast( _dateFormat.parse( value ) );
            }
            catch ( ParseException e )
            {
                return type.cast( new Date() );
            }
        }
        
        throw new EvsException();
    }
    
    public static String convertToString( Object value )
    {
        if ( value instanceof Date )
        {
            return _dateFormat.format( (Date) value );
        }
        
        return value.toString();
    }
    
    private static <T> T _convertEnum( String value, Class<T> type )
    {
        assert type.isEnum();
        value = value.trim();
        
        for ( T e : type.getEnumConstants() )
        {
            if ( e.toString().equals( value ) )
                return e;
        }
        
        return null;
    }
    
    public static <T> T newCollection( Class<T> type )
    {
        assert Collection.class.isAssignableFrom( type );
        
        try
        {
            if ( Set.class.isAssignableFrom( type ) )
                return type.cast( HashSet.class.newInstance() );
    
            if ( Map.class.isAssignableFrom( type ) )
                return type.cast( HashMap.class.newInstance() );
            
            if ( List.class.isAssignableFrom( type ) )
                return type.cast( ArrayList.class.newInstance() );
            
            if ( Collection.class.isAssignableFrom( type ) )
                return type.cast( ArrayList.class.newInstance() );
        }
        catch ( InstantiationException e )
        {
            e.printStackTrace();
        }
        catch ( IllegalAccessException e )
        {
            e.printStackTrace();
        }
        
        return null;
    }
    
    private static SimpleDateFormat _dateFormat = new SimpleDateFormat( "dd.MM.yyyy" );
}
